import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import recommend from '../components/Recommend.vue'
import OrderForm from "../views/OrderForm.vue"
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [
      {
        path: 'attentions',
        name: 'home',
        component: () => import(/* webpackChunkName: "about" */ '../components/Attention.vue'),
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 0,
          atpath: "/home/attentions"
        },
      },
      {
        path: 'recommend',
        name: 'home',
        component: recommend,
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 1,
          atpath: "/home/recommend"
        },
      },
      {
        // 当 /user/:id/posts 匹配成功
        // UserPosts 会被渲染在 User 的 <router-view> 中
        path: 'hot',
        name: 'home',
        component: () => import(/* webpackChunkName: "about" */ '../components/Hot.vue'),
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 2,
          atpath: "/home/hot"
        },
      }
    ],

  },
  {
    path: '/shopping',
    name: 'shopping',

    component: () => import(/* webpackChunkName: "about" */ '../views/Shopping.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true,

    },
    // 详情页面的beforeRouteLeave钩子函数

  },
  {
    path: '/my',
    name: 'my',

    component: () => import(/* webpackChunkName: "about" */ '../views/my.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true
    },
  },
  {
    path: '/classify',
    name: 'classify',

    component: () => import(/* webpackChunkName: "about" */ '../views/Classify.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true
    },
  },
  {
    // 登录
    path: '/login',
    name: 'login',

    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    path: '/register',
    name: 'register',


    component: () => import(/* webpackChunkName: "about" */ '../views/register.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  }, {
    path: '/fenlxiangqing',
    name: 'fenlxiangqing',

    component: () => import(/* webpackChunkName: "about" */ '../components/Classify/fenleixiangq.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true,
      // maksd: false,
    },
  },
  // 订单页面
  {
    path: '/orderform',
    name: 'orderform',
    // redirect: '/Home/Recommend',
    component: OrderForm,


    meta: {
      Yaxis: 0,
      keepAlive: true,
      active: 3,
      atpath: "/orderForm",
      redirect: '/complete'
    },
    children: [
      {
        // 全部订单
        path: 'complete',
        name: 'complete',
        component: () => import(/* webpackChunkName: "about" */ '../components/orderform/complete.vue'),
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 0,
          atpath: "/orderForm/complete"
        },
      },
      {
        // 待付款
        path: 'obligation',
        name: 'obligation',
        component: () => import(/* webpackChunkName: "about" */ '../components/orderform/payment.vue'),
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 1,
          atpath: "/orderForm/obligation"
        },
      },
      {
        // 待发货

        path: 'deliver',
        name: 'deliver',
        component: () => import(/* webpackChunkName: "about" */ '../components/orderform/deliver.vue'),
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 2,
          atpath: "/orderForm/deliver"
        },
      },
      {
        // 待收货
        path: 'take',
        name: 'take',
        component: () => import(/* webpackChunkName: "about" */ '../components/orderform/take.vue'),
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 3,
          atpath: "/orderForm/take"
        },
      },
      {
        // 待评价
        path: 'evaluate',
        name: 'evaluate',
        component: () => import(/* webpackChunkName: "about" */ '../components/orderform/evaluate.vue'),
        meta: {
          Yaxis: 0,
          keepAlive: true,
          active: 4,
          atpath: "/orderForm/evaluate"
        },
      }
    ]
  },
  {
    path: '/orderdetails',
    name: 'orderdetails',
    // redirect: '/Home/Recommend',
    component: () => import(/* webpackChunkName: "about" */ '../views/OrderDetails.vue'),


    meta: {
      Yaxis: 0,
      keepAlive: true,
      active: 3,
      atpath: "/orderdetails"
    },

  },
  {
    path: '/search',
    name: 'search',

    component: () => import(/* webpackChunkName: "about" */ '../views/search.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true,
    },
  },
  // 详情页面
  {
    path: '/details',
    name: 'details',

    component: () => import(/* webpackChunkName: "about" */ '../views/details.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true,
      maksd: false,
    },

  },

  // 评论界面
  {
    path: '/comment',
    name: 'comment',

    component: () => import(/* webpackChunkName: "about" */ '../components/particulars/comment.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true
    },
  },

  // 评论详情
  {
    path: '/pingluxiangq',
    name: 'pingluxiangq',

    component: () => import(/* webpackChunkName: "about" */ '../components/particulars/pingluxiangq.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true
    },
  },
  // 已经评价
  {
    path: '/yipingjia',
    name: 'yipingjia',

    component: () => import(/* webpackChunkName: "about" */ '../views/pimgjia.vue'),
    meta: {
      active: 4,
      Yaxis: 0,
      keepAlive: true,
    },
  },

  {
    // 确定订单页面
    path: '/confirm',
    name: 'confirm',

    component: () => import(/* webpackChunkName: "about" */ '../views/confirm.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    // 地址重新选择页面  列表页面
    path: '/sitellitile',
    name: 'sitellitile',

    component: () => import(/* webpackChunkName: "about" */ '../views/sitellitile.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    // 这个是编辑收货地址
    path: '/selection',
    name: 'selection',

    component: () => import(/* webpackChunkName: "about" */ '../views/selection.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    // 这个是支付页面
    path: '/conpayment',
    name: 'conpayment',
    component: () => import(/* webpackChunkName: "about" */ '../views/payment.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    // 这个是支付页面
    path: '/shuax',
    name: 'shuax',
    component: () => import(/* webpackChunkName: "about" */ '../components/shuaxing.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    // 设置
    path: '/shezhi',
    name: 'shezhi',
    component: () => import(/* webpackChunkName: "about" */ '../views/shezhi.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    // 设置
    path: '/information',
    name: 'information',
    component: () => import(/* webpackChunkName: "about" */ '../views/Information.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  {
    // 评价
    path: '/pingjia',
    name: 'pingjia',
    component: () => import(/* webpackChunkName: "about" */ '../views/pingjia.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: false
    },
  },
  // 收藏 collect
  {

    path: '/collect',
    name: 'collect',
    component: () => import(/* webpackChunkName: "about" */ '../views/collect.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true,
    },
  },
  {
    // 浏览记录
    path: '/browsing',
    name: 'browsing',
    component: () => import(/* webpackChunkName: "about" */ '../views/browsing.vue'),
    meta: {
      Yaxis: 0,
      keepAlive: true,
    },
  },
]
//  this.$store.commit("shoppingActivef", true);
// selection 
const router = new VueRouter({
  routes,
  scrollBehavior(to, from) {
    console.log(to.meta.Yaxis)
    return { x: 0, y: to.meta.Yaxis }
  }
})

router.afterEach((to, from) => {

})

router.beforeEach((to, from, next) => {
  // 路由守卫




  // 点击搜索框的返按钮要返回的位置
  let hq = ["/home/attentions", "/home/hot", "/home/recommend", "home", "my", "classify"]
  for (let index = 0; index < hq.length; index++) {
    if (hq[index] == from.fullPath) {
      localStorage.setItem("search", from.fullPath);
    }

  }

  // localStorage.setItem("frompath", from.fullPath);
  let min = ["/home/attentions", "/home/hot", "/home/recommend"]

  if ((from.fullPath == "/home/recommend" && to.fullPath != "/home/attentions" && to.fullPath != "/home/hot") || (from.fullPath == "/home/attentions" && to.fullPath != "/home/recommend" && to.fullPath != "/home/hot") || (from.fullPath == "/home/hot" && to.fullPath != "/home/recommend" && to.fullPath != "/home/attentions")) {
    localStorage.setItem("ZiLuYou", from.fullPath);
  }




  let historys = localStorage.getItem("ZiLuYou")
  let history = "/home/recommend"
  if (to.fullPath == "/home") {

    for (let index = 0; index < min.length; index++) {
      if (min[index] == historys) {
        return router.push(min[index])
          .catch(() => {
            console.log('all good')
          })
      } else {
        history = "/home/recommend"
      }

    }

    router.push(history)
      .catch(() => {
        console.log('all good')
      })

  }


  // 判断有没有登录


  next()


})


export default router
